/*
 *@(#)ButtonService.java 2013-7-22
 *
 * Copyright 2011 ZhuQiang,All rights reserved
 *
 * author as
 */
package com.xxkj.drugtraceability.scan.alps

import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.IBinder
import android.util.Log
import bn.Handle.HandleSet
import bn.Util.Util

class ButtonService : Service() {
    private var systemReceiver: ButtonReceiver? = null
    private var errorMessage = false
    private var flgReading = false
    private var result: String? = null
    override fun onBind(arg0: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        Log.d(TAG, "**************** ButtonService onCreate")
        systemReceiver = ButtonReceiver()
        systemReceiver!!.register(this)
        openElectricPower()
    }

    fun doReadBar() {
        Thread {
            if (!flgReading) {
                flgReading = true

                try {
                    errorMessage = HandleSet.BarCodeRead()
                } catch (e: Exception) {
                    Log.e(errorMessage.toString(), e.message)
                }
                result = if (!errorMessage) {
                    ""
                } else {
                    Util.BarCodeData
                }
                sendBroadcast(result)
                releaseReadBar()
            }
        }.start()
    }

    fun releaseReadBar() {
        if (flgReading) {
            flgReading = false
            try {
                errorMessage = HandleSet.BarCodeReadStop()
            } catch (e: Exception) {
                Log.e(errorMessage.toString(), e.message)
            }
        }
    }

    // 向应用程序发送读到的条码
    private fun sendBroadcast(result: String?) {
        result?.apply {
            val mIntent = Intent(BAR_READ_ACTION)
            mIntent.putExtra(BAR_READ_KEY, result)
            sendBroadcast(mIntent)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        systemReceiver!!.unregister(this)
        closeElectricPower()
    }

    private fun closeElectricPower() {
        try {
            // 关闭电源
            HandleSet.ModuleDisconnect()
            // 关闭232
            HandleSet.ModuleCommClose()
            // 关闭程序
            // System.exit(0);
        } catch (e: Exception) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }
    }

    private fun openElectricPower() {
        // 打开电源
        try {
//			HandleSet.ModuleCommOpen();
//			HandleSet.ModuleConnect();
            Thread.sleep(100)
        } catch (e: Exception) {
            Log.e("Init", e.message)
        }
    }

    internal inner class ButtonReceiver : BroadcastReceiver() {
        fun register(context: Context) {
            val filter = IntentFilter()
            filter.addAction(BUTTON_ACTION_LEFT)
            filter.addAction(BUTTON_ACTION_RIGHT)
            filter.addAction(ACTION_SCREEN_OFF)
            filter.addAction(ACTION_SCREEN_ON)
            filter.addAction(ACTION_USER_PRESENT)
            context.registerReceiver(this, filter)
        }

        fun unregister(context: Context) {
            context.unregisterReceiver(this)
        }

        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            Log.d(TAG, "*********** action =$action")
            if (BUTTON_ACTION_LEFT.equals(action, ignoreCase = true)
                    || BUTTON_ACTION_RIGHT.equals(action, ignoreCase = true)) {
                doReadBar()
            } else if (ACTION_SCREEN_OFF.equals(action, ignoreCase = true)) {
                closeElectricPower()
            } else if (action.equals(ACTION_USER_PRESENT, ignoreCase = true)
                    || action.equals(ACTION_SCREEN_ON, ignoreCase = true)) {
                openElectricPower();
            }
        }
    }

    companion object {
        private const val TAG = "ButtonService"
        const val BUTTON_ACTION_LEFT = "android.intent.action.SCAN_LEFT" // 按左侧按键的广播
        const val BUTTON_ACTION_RIGHT = "android.intent.action.SCAN_RIGHT" // 按右侧按键的广播
        const val ACTION_SCREEN_OFF = "android.intent.action.SCREEN_OFF" // 屏幕关屏的广播
        const val ACTION_USER_PRESENT = "android.intent.action.USER_PRESENT" // 屏幕解锁的广播
        const val ACTION_SCREEN_ON = "android.intent.action.SCREEN_ON"
        const val BAR_READ_ACTION = "SYSTEM_BAR_READ" // 向Activity发送读到的条码的广播
        const val BAR_READ_KEY = "BAR_VALUE"
    }
}